package com.hlm.temperature_regulate.controller;

import com.hlm.temperature_regulate.WeatherUtil.GetWeather;
import com.hlm.temperature_regulate.dao.WeatherInfo;
import com.hlm.temperature_regulate.mapper.WeatherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;


@RestController
public class WeatherController {
    @Autowired
    private WeatherMapper mapper;

    @PostMapping("/insert")
    /**
     * 方法需要循环插入getweather()的返回值
     */
    public Integer insertWeather(WeatherInfo weather) {
        try {
            String[] dates = GetWeather.getDates();
//            循环插入weather到数据库中
            for (int i = 0; i < dates.length; i++) {
                weather = GetWeather.getWeather(dates[i]);
                mapper.insertWeather(weather);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
